package com.gitee.wsl.android.ui.recyclerView.animation

import android.animation.Animator
import android.animation.ObjectAnimator
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.recyclerview.widget.RecyclerView
import com.gitee.wsl.android.ui.recyclerView.callback.OnViewAttachStateChangeListener
import com.gitee.wsl.android.ui.recyclerView.callback.ViewAttachStateChangeListenerContainer

open class AdapterAnimationHelper : OnViewAttachStateChangeListener {

    protected var mLastPosition = -1

    /**
     * Whether enable animation.
     * 是否打开动画
     */
    var animationEnable: Boolean = false

    /**
     * Whether the animation executed only the first time.
     * 动画是否仅第一次执行
     */
    var isAnimationFirstOnly = true

    /**
     * Set custom animation.
     * 设置自定义动画
     */
    var itemAnimation: ItemAnimator? = null
        set(value) {
            animationEnable = true
            field = value
        }

    /*private var itemAnimationType:AnimationType = AnimationType.AlphaIn


    fun setItemAnimationType(type: AnimationType){
        itemAnimationType=type

    }*/

    fun bandinAdapter(adapter: ViewAttachStateChangeListenerContainer){
        adapter.addOnViewAttachStateChangeListener(this)
        mLastPosition = -1
    }

    fun unbandinAdapter(adapter: ViewAttachStateChangeListenerContainer){
        adapter.removeOnViewAttachStateChangeListener(this)
    }

    fun runAnimator(holder: RecyclerView.ViewHolder) {
        if (animationEnable) {
            if (!isAnimationFirstOnly || holder.layoutPosition > mLastPosition) {
                if(itemAnimation!=null)
                    itemAnimation!!.animator(holder.itemView).apply {
                        startItemAnimator(this, holder)
                    }
                else
                    applyCustomAnimation(holder)
                mLastPosition = holder.layoutPosition
            }
        }
    }

    /**
     * 用于 自定义视图 动画
     *    view.animation{
     *        scaleIn()
     *        slideInBottom()
     *    }
     */
   protected fun applyCustomAnimation(holder: RecyclerView.ViewHolder){
        AlphaInAnimation().animator(holder.itemView).apply {
            startItemAnimator(this, holder)
        }
    }

    private fun startItemAnimator(anim: Animator, holder: RecyclerView.ViewHolder) {
        anim.start()
    }

    override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
       runAnimator(holder)
    }

    override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder) {
    }
}

/**
 * 内置默认动画类型
 */
enum class AnimationType {
    AlphaIn, ScaleIn, SlideInBottom, SlideInLeft, SlideInRight
}

/**
 * https://github.com/CymChad/BaseRecyclerViewAdapterHelper
 */
interface ItemAnimator {
    fun animator(view: View): Animator
}

/**
 * An animation to fade item in, changing alpha from default 0f to 1.0f at a uniform rate in default 300ms.
 *
 * item 淡入显示的动画
 */
class AlphaInAnimation @JvmOverloads constructor(
    private val duration: Long = 300,
    private val mFrom: Float = DEFAULT_ALPHA_FROM
) : ItemAnimator {

    private val interpolator = LinearInterpolator()

    override fun animator(view: View): Animator {
        val animator = ObjectAnimator.ofFloat(view, "alpha", mFrom, 1f)
        animator.duration = duration
        animator.interpolator = interpolator
        return animator
    }

    companion object {
        private const val DEFAULT_ALPHA_FROM = 0f
    }

}